package y24.m5.file;

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentSystem{
    private final static String STUDENT_FILE_PATH = "student.txt";
    private final static String SCORE_FILE_PATH = "score.txt";
    private final static String MERGE_FILE_PATH = "merge.txt";


    public void menu1(){
        System.out.println("******************************");
        System.out.println("*****    1、录入学生信息    *****");
        System.out.println("*****    2、退出系统       *****");
        System.out.println("******************************");
    }
    public void menu2(){
        System.out.println("-------------------------------");
        System.out.println("          1、重新输入");
        System.out.println("          2、继续输入");
        System.out.println("          3、返回上一步");
        System.out.println("-------------------------------");
    }
    public List<Student> creatStudenArray(){
        List<Student> list = new ArrayList<>();
        return list;
    }
    public Boolean readStudentFile(){
        File file = new File(STUDENT_FILE_PATH);
        return file.exists() && !file.isDirectory() && file.length() == 0;
    }
    public Boolean inputStudent(List<Student> list){
        while (scannerStudent(list));
        getInsertStudent(list).inputFile();
        return countMerge();
    }
    public Boolean addStudent(List<Student> list){
        while (scannerStudent(list));
        getInsertStudent(list).appFile();
        return countMerge();
    }
    private Boolean countMerge(){
        getCountScore().count();
        return getMergeAccount().merge();
    }
    private InsertStudent getInsertStudent(List<Student> list){
        File file = new File(STUDENT_FILE_PATH);
        return new InsertStudent(file,list);
    }
    private CountScore getCountScore(){
        File student = new File(STUDENT_FILE_PATH);
        File score = new File(SCORE_FILE_PATH);
        return new CountScore(student,score);
    }
    private MergeAccount getMergeAccount(){
        File student = new File(STUDENT_FILE_PATH);
        File score = new File(SCORE_FILE_PATH);
        File file = new File(MERGE_FILE_PATH);
        return new MergeAccount(student,score,file);
    }
    private Boolean scannerStudent(List<Student> list){
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        if ("#".equals(name)){
            return false;
        }
        String id = scanner.next();
        BigDecimal score = scanner.nextBigDecimal();
        list.add(new Student(id,name,score));
        return true;
    }
}